package com.lianbang.staff.vm

import android.widget.Switch
import androidx.lifecycle.LiveData
import cn.ubest.mvvmcore.base.BaseListViewModel
import cn.ubest.mvvmcore.bean.ResultState
import cn.ubest.mvvmcore.interfaces.RequestHandler1Impl
import com.lianbang.staff.bean.ShopDetailBean
import com.lianbang.staff.bean.ShopListBean
import com.lianbang.staff.bean.ShopOrderListBean
import com.lianbang.staff.model.ShopModel

/**
 * Created by maoqi on 2021/1/14.
 */
class ShopVM : BaseListViewModel<ShopModel>() {

    companion object {
        const val TYPE_SHOP_LIST = 1
        const val TYPE_SHOP_ORDER_LIST = 2
    }

    override fun loadData(isInit: Boolean, vararg arg: Any) {
        when (arg[0] as Int) {
            TYPE_SHOP_LIST -> {
                val keyword = if (arg.size > 1) {
                    (arg[1] as String)
                } else {
                    ""
                }
                getShopList(keyword)
            }
            TYPE_SHOP_ORDER_LIST -> {
                if (arg.size > 1) {
                    (arg[1] as Int)
                } else {
                    null
                }?.also {
                    getShopOrderList(it)
                }
            }
        }
    }

    private fun getShopList(keyword: String) {
        requestWithLife(object : RequestHandler1Impl<List<ShopListBean>>(this) {
            override suspend fun request1(): List<ShopListBean> {
                return mModel.getShopList(keyword, curPageNum, pageSize, "").resolveWrapper()
            }

            override fun onSuccess(t: List<ShopListBean>) {
                setData(t)
            }
        })
    }

    private fun getShopOrderList(uid: Int) {
        requestWithLife(object : RequestHandler1Impl<List<ShopOrderListBean>>(this) {
            override suspend fun request1(): List<ShopOrderListBean> {
                return mModel.getShopOrderList(uid, "", curPageNum, pageSize, "")
                    .resolveWrapper()
            }

            override fun onSuccess(t: List<ShopOrderListBean>) {
                setData(t)
            }
        })
    }

    fun getShopDetail(uid: Int) {
        requestWithLife(object : RequestHandler1Impl<ShopDetailBean>(this) {
            override suspend fun request1(): ShopDetailBean {
                return mModel.getShopDetail(uid).resolveWrapper()
            }

            override fun onSuccess(t: ShopDetailBean) {
                mLiveData.postValue(t)
            }
        })
    }
}